import { LVector3 } from "../../Moduls/Tools/LMath";
import GameHandler, { BufType } from "../GameHandler";


export default class BufAction extends Laya.Script {
    public DetecTarget: Laya.Sprite3D;
    private self:Laya.Sprite3D;

    private downPos:Laya.Vector3;
    private upPos:Laya.Vector3;
    private halfPeriod:number;
    private direction:number;

    constructor() {
        super();
    }

    onAwake() {
        this.self = this.owner as Laya.Sprite3D;
        this.downPos = this.self.transform.position.clone();
        this.upPos =  this.downPos.clone();
        this.upPos.y += 0.5;

        this.direction = 1;
        this.halfPeriod = 4000;
    }

    onStart() {
    }

    onUpdate() {
        if( Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < GameHandler.BufTrigDistance &&
        Math.abs(this.self.transform.position.x-GameHandler.Player.transform.position.x) < GameHandler.BufTrigDistance)
        {
            if(Laya.Vector3.distance(this.self.transform.position,GameHandler.Player.transform.position) < GameHandler.BufTrigDistance){
                this.self.active = false;
                Laya.stage.event("BufTrig", this.self.name);
            }
        }
        if(this.direction == 1){
            this.direction = 0;
            this.DoUpMove();
        }
        else if(this.direction == -1){
            this.direction = 0;
            this.DoDownMove();
        }

        if(this.direction == 0){
            this.self.transform.position = this.pos;
        }
    }
    private pos:Laya.Vector3;
    private tween: Laya.Tween;
    public DoUpMove(){
        //this.tween && this.tween.clear();
        this.pos = this.downPos.clone();
        this.tween = Laya.Tween.to(this.pos,{x:this.upPos.x,y:this.upPos.y,z:this.upPos.z},1000,Laya.Ease.sineInOut,new Laya.Handler(this,()=>{this.direction= -1;}));
    }
    public DoDownMove(){
        //this.tween && this.tween.clear();
        this.pos = this.upPos.clone();
        this.tween = Laya.Tween.to(this.pos,{x:this.downPos.x,y:this.downPos.y,z:this.downPos.z},1000,Laya.Ease.sineInOut,new Laya.Handler(this,()=>{this.direction= 1;}));
    }
}